<?php
/**
 *
 * 版权所有：忠赢网站管理系统<www.51shunshi.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Home\Controller;

use Vendor\Page;
class IndexController extends ComController
{
    public function index()
    {
        // 最新四条新闻
        // http://demo.weboss.hk/w074/page/service/feedback.php
        $article = M('article as a');
        $prefix = C('DB_PREFIX');
        $orderby = "a.aid DESC";
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category.name,{$prefix}category.dir";
        $news = $article->field($field)
            ->order($orderby)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->limit(4)
            ->select();
        $this->assign('news',$news);
        // 产品
        $product = M('case as a');
        $product_orderby = "a.aid DESC";
        $field_product = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category_case.name,{$prefix}category_case.dir";
        $product_list = $product->field($field_product)
            ->order($product_orderby)
            ->join("{$prefix}category_case ON {$prefix}category_case.id = a.sid")
            ->limit(8)
            ->select();
        $this->assign('product',$product_list);
        // 轮播焦点图
        $loop = M('flash as a');
        $orderby = "a.o ASC";
        $field_loop = "a.title,a.url,a.pic";
        $loop_list = $loop->field($field_loop)
            ->where("a.pid = 1")
            ->order($orderby)
            ->select();
        $this->assign('loop_list',$loop_list);
		$this->display();
	}

    /**
     * 新闻
     */
    public function news(){

//        pp($_SERVER);exit();
        $p = intval(I('p')) > 0 ? I('p') : 1;
        $cid = intval(I('id')) > 0 ? I('id') : 37;
        $article = M('article as a');
        if(!empty($cid)){
            $where = "sid = $cid";
        }else{
            $where = "1 = 1";
        }
        $categoryName = M("category")->where("id= $cid")->getField("name");
        $this->assign('categoryName',$categoryName);
        // 贷款资讯中间
        $orderby = "a.aid DESC";
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category.name,{$prefix}category.dir";
        $count = $article->where($where)->count();
        $list = $article->field($field)
            ->where($where)
            ->order($orderby)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->limit($offset . ',' . $pagesize)
            ->select();
//        echo $article->getlastsql();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
//        pp($list);exit();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $category = M("category")->select();
        $this->assign('category',$category);
        $this->display();
    }

    //单页
    public function page(){

        $aid = intval(I('id')) > 0 ? I('id') : false;
        if(!$aid){
            $this->display("404");
        }
        $article = M('article')->where('aid='.$aid)->find();
        $this->assign('article',$article);
        $this->assign('nav',$aid);
        $this->display();
    }

    public function about(){
        $this->display();
    }


    public function service(){
        $id = intval(I('id')) > 0 ? I('id') : 1;
        if($id == 1){
            $this->display();
        }
        if($id == 2){
            $this->display('service_sem');
        }
        if($id == 3){
            $this->display('service_shop');
        }
        if($id == 4){
            $this->display('service_system');
        }
        if($id == 5){
            $this->display('service_weixin');
        }
    }

    public function caselist(){
        $p = intval(I('p')) > 0 ? I('p') : 1;
        $cid = intval(I('id')) > 0 ? I('id') : 1;
        $article = M('case as a');
        $where = "sid = $cid";
        $categoryName = M("category_case")->where("id= $cid")->getField("name");
        $this->assign('categoryName',$categoryName);
        // 贷款资讯中间
        $orderby = "a.aid DESC";
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category_case.name,{$prefix}category_case.dir";
        $count = $article->where($where)->count();
        $list = $article->field($field)
            ->where($where)
            ->order($orderby)
            ->join("{$prefix}category_case ON {$prefix}category_case.id = a.sid")
            ->limit($offset . ',' . $pagesize)
            ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list',$list);
        $this->assign('page',$page);
//        pp($list);exit();
        $this->display('case');
    }
    public function caseinfo(){
        $aid = intval(I('id')) > 0 ? I('id') : false;
        if(!$aid){
            $this->display("404");
        }
        $case = M('case')->where('aid='.$aid)->find();
        $this->assign('case',$case);
        $this->assign('nav',$aid);
        if(isMobile()){
            $this -> display("mobile/page");
        }else{
            $this->display('case_show');
        }
    }

    public function sessions(){
        pp($_SESSION);
        pp(cookie('auth'));
    }

    public function product(){
        // 产品
        $prefix = C('DB_PREFIX');
        $product = M('case as a');
        $product_orderby = "a.aid DESC";
        $field_product = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category_case.name,{$prefix}category_case.dir";
        $product_list = $product->field($field_product)
            ->order($product_orderby)
            ->join("{$prefix}category_case ON {$prefix}category_case.id = a.sid")
            ->limit(8)
            ->select();
        $this->assign('product',$product_list);
        $this->display();
    }

    public function detail(){

        $aid = intval(I('id')) > 0 ? I('id') : false;
        if(!$aid){
            $this->display("404");
        }
        $article = M('case')->where('aid='.$aid)->find();
        $this->assign('article',$article);
        $this->assign('nav',$aid);
        $this->display();
    }

    public function contact(){
        $this->display();
    }
}